package com.fasttreatment.zzr.demo.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.androidlibrary.base.BaseActivity;
import com.android.androidlibrary.router.Router;
import com.fasttreatment.zzr.demo.R;
import com.fasttreatment.zzr.demo.myrouter.UrlRouter;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;


/**
 * Created by zzr on 17/1/12.
 */

public class QrcodeActivity extends BaseActivity implements QRCodeView.Delegate {
    @BindView(R.id.zxingview)
    ZXingView mZxingview;
    @BindView(R.id.radio_light)
    Button mRadioLight;
    @BindView(R.id.toolbar_left_btn)
    Button mToolbarLeftBtn;
    @BindView(R.id.toolbar_right_btn)
    Button mToolbarRightBtn;
    ;

    private boolean isOn = false;

    @Override
    public void initData(Bundle savedInstanceState) {
        mZxingview.setDelegate(this);
        mZxingview.startSpotDelay(0);
    }

    @Override
    public void setListener() {
        mRadioLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isOn) {
                    mZxingview.closeFlashlight();
                    mRadioLight.setBackgroundResource(R.mipmap.ic_launcher);
                    isOn = false;
                } else {
                    mZxingview.openFlashlight();
                    mRadioLight.setBackgroundResource(R.mipmap.ic_launcher);
                    isOn = true;
                }
            }
        });
        mToolbarRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UrlRouter.goCreateQrcode(context);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_qrcode;
    }



    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        mZxingview.stopSpot();
        Intent intent = new Intent();
        intent.putExtra("result", result);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(context, "打开相机出错", Toast.LENGTH_SHORT).show();

    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mZxingview.startCamera();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mZxingview.startSpotAndShowRect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mZxingview.stopSpotAndHiddenRect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mZxingview.stopCamera();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mZxingview.onDestroy();
    }


}
